package com.atguigu.gmall.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.CategoryMapper;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import com.atguigu.gmall.pms.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<CategoryEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<CategoryEntity> queryCategoriesByParentId(Long parentId) {
        LambdaQueryWrapper<CategoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        if(parentId!=-1){
            //当做pid查找下一级分类
            queryWrapper.eq(CategoryEntity::getParentId , parentId);
        }
        return this.list(queryWrapper);
    }

    @Override
    public List<CategoryEntity> queryCatesAndsSubs(Long c1id) {
        return baseMapper.queryCatesAndsSubs(c1id);
    }

    @Override
    public List<CategoryEntity> queryItemLevel123CatesByC3id(Long c3id) {
        CategoryEntity c3 = baseMapper.selectById(c3id);
        CategoryEntity c2 = baseMapper.selectById(c3.getParentId());
        CategoryEntity c1 = baseMapper.selectById(c2.getParentId());
        return Arrays.asList(c1,c2,c3);
    }

}